<template>
  <div class="app-container" style="padding: 10px 20px;">
    <div class="filter-container">
      <el-button class="filter-item" style="margin: 0;" type="primary" icon="el-icon-edit" size="small" @click="handleCreate">
        新增
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id" align="center" />
      <el-table-column label="设备名称" prop="device_name" align="center" />
      <el-table-column label="设备编号" prop="device_num" align="center" />
      <el-table-column label="设备状态" class-name="status-col">
        <template v-slot="{row}">
          <el-tag :type="row.device_status | typeFilter">{{ row.device_status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="设备所在区" prop="device_area" align="center" />
      <el-table-column label="操作" align="center" width="80" class-name="small-padding fixed-width">
        <template v-slot="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="100px">
        <el-form-item label="设备名称" prop="device_name">
          <el-input v-model="temp.device_name" />
        </el-form-item>
        <el-form-item label="设备编号" prop="device_num">
          <el-input v-model="temp.device_num" />
        </el-form-item>
        <el-form-item label="设备所在区" prop="device_area">
          <el-input v-model="temp.device_area" />
        </el-form-item>
        <el-form-item label="设备状态" prop="device_status">
          <el-input v-model="temp.device_status" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addDevice, getDevice, saveDevice } from '@/api/data'
import Pagination from '@/components/Pagination'

export default {
  name: 'Equipment',
  components: { Pagination },
  filters: {
    typeFilter(status) {
      const typeMap = {
        '在线': 'success',
        '离线': 'info',
        '异常': 'danger'
      }
      return typeMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      temp: {
        'device_name': '',
        'device_num': '',
        'device_area': '',
        'device_status': ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      rules: {
        device_name: [{ required: true, message: '设备名称不能为空', trigger: 'blur' }],
        device_num: [{ required: true, message: '设备编号不能为空', trigger: 'blur' }],
        device_area: [{ required: true, message: '设备所在区不能为空', trigger: 'blur' }],
        device_status: [{ required: true, message: '设备状态不能为空', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getDevice(this.listQuery).then(res => {
        this.list = res.result.data
        this.total = res.result.total
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作Success',
        type: 'success'
      })
      row.status = status
    },
    resetTemp() {
      this.temp = {
        'device_name': '',
        'device_num': '',
        'device_area': '',
        'device_status': ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addDevice(this.temp).then((res) => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '新增设备信息成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          saveDevice(tempData).then((res) => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '设备信息更新成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    }
  }
}
</script>
